/*
 * ========================================================
 * Copyright(c) 2014 杭州龙骞科技-版权所有
 * ========================================================
 * 本软件由杭州龙骞科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * http://www.hzdracom.com/
 * ========================================================
 */
package com.shwread.httpsdk.http.face;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONObject;

import logic.bean.ColumnInfos;
import logic.bean.ReadBook;
import logic.constants.AppConfig;
import logic.constants.Const;
import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shwread.httpsdk.constant.ResultCode;
import com.shwread.httpsdk.http.base.AbstractHttpPostDracom;
import com.shwread.httpsdk.http.callback.ActionListener;


/**
 * 查询最近阅读书籍列表
 * @author 刘宾  
 * @date 2014年11月11日 上午10:13:36
 */
public class QryReadBookListAction
        extends
        AbstractHttpPostDracom
{
	private Long id; //客户端查询增量数据时必填，需传递客户端本地库存的最大有效id以便服务端做比对
	private int  pageNum;
	private int  length;
	
	/**
	 * 查询最近阅读书籍列表
	 * @param context
	 * @param id 客户端查询增量数据时必填，需传递客户端本地库存的最大有效id以便服务端做比对
	 * @param pageNum
	 * @param length
	 * @param actionListener
	 */
	public QryReadBookListAction(Context context, Long id, int pageNum, int length, ActionListener actionListener) {
		super(context, "qryReadBookList.do", actionListener);
		this.id = id;
		this.pageNum = pageNum;
		this.length = length;
	}
	
	@Override
	public void getParameter(HashMap<String, String> parameter_map) {
		parameter_map.put("enterpriseId", AppConfig.getEnterpriseId());
		parameter_map.put("pageNum", String.valueOf(pageNum));
		parameter_map.put("length", String.valueOf(length));
		parameter_map.put("id", String.valueOf(id));
		parameter_map.put("account", Const.phone_number);
		parameter_map.put("timeStamp", String.valueOf(timeStamp));
		parameter_map.put("cid", cid(Const.phone_number + Const.ACCOUNTTYPE + Const.CLIENTTYPE + timeStamp + AppConfig.getEnterpriseId() + pageNum + length ));
	}
	
	@Override
	public void doResponseXml(String xml) {
		println("QryReadBookListAction --> "+xml);
		int result = -1;
		try
		{
			JSONObject jsonObject = new JSONObject(xml);
			result = jsonObject.getInt("resultCode");
			if (result == 1000)
			{
				List<ReadBook> books = new ArrayList<ReadBook>();
				Gson gson = new Gson();
				final java.lang.reflect.Type type = new TypeToken<List<ReadBook>>(){}.getType();
				books = gson.fromJson(jsonObject.getJSONObject("returnObject").getString("readBookList"), type);
				listener.OK(books);
			}
			else
			{
				listener.ERROR(result, ResultCode.getResultText(result));
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	@Override
	protected void doRequestHeader(HashMap<String, String> header_map) {
		
	}
	
	@Override
	public void dispose() {
		
	}
	
}
